import { defineStore } from 'pinia'
import { getFileList, getSearch } from '@/service/api'


export default defineStore('file', {
  state() {
    return {
      list: [],
      searchType: "all",
    }
  },
  persist: true,
  getters: {
    getFormatList(state) {
      return state.list.map((item: any) => {
        item.type = item.ext === '' ? 'dir' : item.ext.substring(0, item.ext.lastIndexOf('/'))
        item.checked = false
        return item
      })
    }
  },
  actions: {
    async getFileList(payload: { file_id?: number, orderby?: string }) {

      const url = `file_id=${payload?.file_id || 0}&orderby=${payload?.orderby || 'name'}&type=${this.searchType}`
      const res = await getFileList(url)
      this.list = res.data.data
    },
    async getSearchList(keyword: string) {
      const res = await getSearch(keyword)
      this.list = res.data.data
    },
    async changeType(type: string) {
      this.searchType = type
    },
  }
})